//
//  YMFMyViewController.swift
//  FlyingPigeon
//
//  Created by yang on 2019/5/22.
//  Copyright © 2019年 a. All rights reserved.
//

import Foundation
import UIKit

class YMFMyinfoCell:UITableViewCell {
     @IBOutlet weak var iconImage: UIImageView!
     @IBOutlet weak var title: UILabel!
    
}


class YMFMyViewController: UIViewController {
    let tableView = UITableView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.title = "我的"
        
        tableView.dataSource = self
        tableView.delegate = self
        tableView.register(UINib(nibName: "YMFMyinfoCell", bundle: nil), forCellReuseIdentifier: "cell")
        tableView.backgroundColor = UIColor(red: 245 / 255.0, green: 245 / 255.0, blue: 245 / 255.0, alpha: 1)
        tableView.frame = CGRect(x: 0, y: 0, width: view.bounds.width, height: view.bounds.height - 64)
        view.addSubview(tableView)
        
        tabBarItem = UITabBarItem(title: "我的", image:  UIImage(named: "icon_car"), tag: 111)
    }
}

extension YMFMyViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        let view = UIView(frame: CGRect(x: 0, y: 0, width: tableView.bounds.width, height: 130))
        view.backgroundColor = UIColor(red: 96 / 255.0, green: 149 / 255.0 , blue: 223 / 255.0, alpha: 1)
        
        let userImageView = UIImageView(frame: CGRect(x: 10, y: 0, width: 78, height: 78))
        userImageView.image = UIImage(named: "userImage")
        userImageView.center = CGPoint(x: userImageView.center.x, y: view.bounds.height * 0.6)
        view.addSubview(userImageView)
        
        let userName = UILabel(frame: CGRect(x: userImageView.frame.maxX + 10, y: userImageView.center.y - 30, width: 200, height: 21))
        userName.font = UIFont.systemFont(ofSize: 14)
        userName.textColor = UIColor.white
        userName.text = UserDefaults.standard.value(forKey: "userName") as? String
        view.addSubview(userName)
        
        let userInfo = UILabel(frame: CGRect(x: userImageView.frame.maxX + 10, y: userImageView.center.y , width: 200, height: 21))
        userInfo.font = UIFont.systemFont(ofSize: 14)
        userInfo.textColor = UIColor.white
        userInfo.text = "车龄：" + (UserDefaults.standard.value(forKey: "year") as! String)
        view.addSubview(userInfo)
        
        
        return view
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 130
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as? YMFMyinfoCell
        
        if indexPath.row == 0 {
            cell?.iconImage.image = UIImage(named: "info_icon")
            cell?.title.text = "个人资料"
        } else if indexPath.row == 1 {
            cell?.iconImage.image = UIImage(named: "login_icon")
            cell?.title.text = "退出登录"
        }
        
        return cell!
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        tableView.deselectRow(at: indexPath, animated: true)
        
        if indexPath.row == 0 {
            let vc = YMFUserInfoViewController()
            navigationController?.pushViewController(vc, animated: true)
        } else if indexPath.row == 1 {
            UIApplication.shared.keyWindow?.rootViewController = YMFLoginViewController()
            
        }
    }
}
